package year2021.month08910;

import java.util.Random;

public class SortTest {
    public static void main(String[] args){
//        int[] origin = {54,76,3,74,1,97,42,85,90,20};
        int[] origin = {54,76,3,74,1,97,42,85,90,20,75};
//        int[] origin = {54,76,3,74,1,97,42,85,90,20,75,42};

        long begin = System.currentTimeMillis();

        int[] monkeySort = monkeySort(origin);

        long end = System.currentTimeMillis();

        System.out.printf("耗时：%ds", (end-begin)/1000);
        System.out.println();

        for (int i=0; i<monkeySort.length; i++){
            System.out.print(monkeySort[i]+ "\t");
        }
    }

    public static int[] monkeySort(int[] origin){
        int[] monkeySort = new int[origin.length];
        Random ran = new Random();

        while (true){
            for (int i=0; i<origin.length; i++){
                while (true){
                    int nextInt = ran.nextInt(origin.length);
                    if (monkeySort[nextInt]==0){
                        monkeySort[nextInt] = origin[i];
                        break;
                    }
                }
            }

            boolean flag = true;

            for (int i=0; i<monkeySort.length-1; i++){
                if (monkeySort[i]>monkeySort[i+1]){
                    flag = false;
                    break;
                }
            }

            if (flag){
                break;
            } else {
                monkeySort = new int[origin.length];
            }
        }

        return monkeySort;
    }

}
